<template>
  <div class="detail">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav" />
    <scroll
      class="content"
      ref="scroll"
      @getScrollPosition="contentScroll"
      :probeType="3"
    >
      <detail-swiper :topImages="topImages" />
      <detail-base-info :goods="goods" />
      <detail-shop-info :shop="shop" />
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad" />
      <detail-param-info :param-info="paramInfo" ref="params" />
      <detail-comment-info :comment-info="commentInfo" ref="comment" />
      <goods-list :goodList="recommends" ref="recommends" />
    </scroll>
    <back-top @click.native="backClick" v-show="isShow" />
    <detail-bottom-bar @addCart="addCart" />
    <toast :message="showMessage" :isShow="isShow"/>
  </div>
</template>

<script>
import DetailNavBar from "./childComps/DetailNavBar.vue";
import DetailSwiper from "./childComps/DetailSwiper";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import Scroll from "components/common/scroll/Scroll.vue";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInfo from "./childComps/DetailParamInfo";
import DetailCommentInfo from "./childComps/DetailCommentInfo";
import GoodsList from "components/content/goods/GoodList";
import DetailBottomBar from "./childComps/DetailBottomBar";
import { debounce } from "components/common/utils";
import Toast from "components/common/toast/Toast"
import { itemListenerMixin, backTopMinin } from "@/common/mixin";
import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend,
} from "network/detail.js";
export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    Toast,
  },
  mixins: [itemListenerMixin, backTopMinin],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      positionY: null,
      currentIndex: 0,
      showMessage:"",
      isShow:false,


    };
  },
  created() {
    //请求详情数据
    this.iid = this.$route.params.iid;
    getDetail(this.iid).then((res) => {
      const data = res.result;
      this.topImages = data.itemInfo.topImages;
      //获取详情页商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      //获取店铺信息
      this.shop = new Shop(data.shopInfo);
      //保存商品详情数据
      this.detailInfo = data.detailInfo;
      // 保存参数信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );
      //取出评论信息
      if (data.rate.list) {
        this.commentInfo = data.rate.list[0];
      }
    });

    //请求推荐数据
    getRecommend().then((res) => {
      this.recommends = res.data.list;
    });
    //给getThemeTopY赋值
    this.getThemeTopY = debounce(() => {
      this.themeTopYs = [];
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.params.$el.offsetTop);
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      this.themeTopYs.push(this.$refs.recommends.$el.offsetTop);
    });
  },

  methods: {
    imageLoad() {
      this.$refs.scroll.refresh();
      this.getThemeTopY();
    },
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 100);
    },
    contentScroll(position) {
      this.demo(position);
      //获取Y坐标值
      const positionY = -position.y;
      //与theme中的值对比
      let length = this.themeTopYs.length;
      for (let i = 0; i < length; i++) {
        //判断是否在最后一个theme
        if (
          this.currentIndex !== length - 1 &&
          positionY > this.themeTopYs[length - 1]
        ) {
          this.currentIndex = length - 1;
          console.log(this.currentIndex);
          this.$refs.nav.currentIndex = this.currentIndex;
          return;
        } else if (
          this.currentIndex !== i &&
          positionY > this.themeTopYs[i] &&
          positionY < this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;
          console.log(this.currentIndex);
          this.$refs.nav.currentIndex = this.currentIndex;
          return;
        }
      }
    },
    addCart(){
     //获取购物车需要展示的信息
     const product = {}
     product.image = this.topImages[0];
     product.title = this.goods.title;
     product.desc = this.goods.desc;
    product.price=  this.goods.realPrice;
    product.iid = this.iid;
    //将商品添加到购物车
    this.$store.dispatch("addCart",product).then(res => {
      this.isShow = true;
      this.showMessage = res;
      setTimeout(() => {
        this.isShow = false;
        this.showMessage = ""
      }, 1000);
    })
    
    //添加购物车成功

    },
  },
  mounted() {},
  destroyed() {
    this.$bus.$off("itemImageLoad", this.itemImageListener);
  },
};
</script>

<style scoped>
.detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}
.content {
  height: calc(100% - 44px);
}
.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #fff;
}
</style>